﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NFine.Data;
using NFine.Domain._03_Entity.Biz;
using NFine.Domain._04_IRepository.Biz;

namespace NFine.Repository.Biz
{
    public class MemberBonusRepository : RepositoryBase<MemberBonusEntity>, IMemberBonusRepository
    {
        public decimal? GetMemberCurMonthBonus(string memberId)
        {
            string sql =
                string.Format(
                    "SELECT SUM(F_CurAmount) as result from  biz_member_bonus WHERE F_MemberId = '{0}' and F_BonusType < 10 and  date_format(F_BonusTime, '%Y-%m') = date_format(CURDATE( ), '%Y-%m')",
                    memberId);
            return GetSingleToDecimal(sql);
        }

        public decimal GetMemberCurAmount(string memberId) {
            string sql =
                string.Format(
                    "SELECT F_CurAmount as result from  biz_member_bonus WHERE F_MemberId = '{0}' order by F_BonusTime desc  LIMIT 1",
                    memberId);
            return GetSingleToDecimal(sql);
        }
        public decimal GetMemberSumAmount(string memberId)
        {
            string sql =
                string.Format(
                    "SELECT sum(F_BonusAmount) as result from  biz_member_bonus WHERE F_MemberId = '{0}' and F_BonusAmount>0",
                    memberId);
            return GetSingleToDecimal(sql);
        }

        public decimal GetMemberSumAmountForMonth(string memberId) {
            string sql =
                string.Format(
                    "SELECT sum(F_BonusAmount) as result from  biz_member_bonus WHERE F_MemberId = '{0}' and F_BonusAmount>0 and  date_format(F_BonusTime, '%Y-%m') = date_format(CURDATE( ), '%Y-%m')",
                    memberId);
            return GetSingleToDecimal(sql);
        }
    }
}
